CMU 15-112 Summer 2019: Fundamentals of Programming and Computer Science
Homework 4 (Due Wed 29-May, at 5pm)
- This assignment is SOLO. This means you may not look at other student's code or let other students look at your code for these problems. See the syllabus for details.
- To start:
- Create a folder named 'week2'
- Download hw4.py to that folder
- Edit hw4.py using Pyzo
- When you are ready, submit hw4.py to Autolab. For this hw, you may submit up to 20 times, but only your last submission counts.
- Do not use recursion in this assignment.
- Do not hardcode the test cases in your solutions.
- This assignment gives you very few test cases in the starter file. You'll want to add your own (especially since test cases are part of the style guidelines)!
- This homework is graded for style. For hw4 and hw5, however, every style violation will be halved, and the maximum total deduction you can get for style is capped at 5.
- Attend a small group [5pts]
By quiz time (Friday 5:30pm), attend a small group session hosted by one of our TAs. Note that this does not need to be done by the homework deadline - you can attend any session as long as it is before the quiz on Friday.
- lookAndSay(a) [35pts]
First, read about look-and-say numbers here. Then, write the function lookAndSay(a) that takes a list of numbers and returns a list of numbers that results from "reading off" the initial list using the look-and-say method, using tuples for each (count, value) pair. For example:lookAndSay([]) == [] lookAndSay([1,1,1]) == [(3,1)] lookAndSay([-1,2,7]) == [(1,-1),(1,2),(1,7)] lookAndSay([3,3,8,-10,-10,-10]) == [(2,3),(1,8),(3,-10)]
Hint: you'll want to keep track of the current number and how many times it has been seen. - nondestructiveRotateList(a, n) [30pts]
Write the function nondestructiveRotateList(a, n) which takes a list a and an integer n, and nondestructively modifies the list so that each element is shifted to the right by n indices (including wraparound). The function should then return this new list. For example:nondestructiveRotateList([1,2,3,4], 1) -> [4,1,2,3] nondestructiveRotateList([4,3,2,6,5], 2) -> [6, 5, 4, 3, 2] nondestructiveRotateList([1,2,3], 0) -> [1,2,3] nondestructiveRotateList([1, 2, 3], -1) -> [2, 3, 1]
- destructiveRotateList(a, n) [30pts]
This function works the same as the previous function, only here it is destructive. That is, it directly changes the list a, so after the call, that exact list is rotated n indices to the right with wraparound, and a new list is not created. As usual for destructive functions, this function returns None. Also: you may not call the nondestructive version here, and in fact, you may not even create a new list (or tuple or other similar data structure) that is longer than 10 elements (you must modify the original list in place).
Hint: the destructive list methods will be very helpful for this problem.